//
// Created by guanyicheng on 2022/3/11.
//

#include "../../common.h"

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr) {
            return head;
        }
        ListNode *pre = nullptr, *curr = head, *temp;
        head = head->next == nullptr ? head : head->next;
        while (curr && curr->next) {
            temp = curr->next;
            curr->next = temp->next;
            temp->next = curr;
            if (pre) {
                pre->next = temp;
            }
            pre = curr;
            curr = curr->next;
        }
        return head;
    }
};

int main() {

}